package com.spider;

import com.util.CheckStringIsNull;
import com.util.ValidateRule;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 核心搜索类
 *
 * @author Conceit
 * @version 1.0, 2017/1/11
 */
public class CoreExtractSerach {

    public  static List<LinkTypeData> extract(Rule rule) {
        //对rule进行校验
        new ValidateRule().validataRule(rule);
        List<LinkTypeData> datas = new ArrayList<LinkTypeData>();
        LinkTypeData linkTypeData = null;


        //解析rule
        String url = rule.getUrl();
        String[] params = rule.getParams();
        String[] values = rule.getValues();
        String resultTagName = rule.getResultTagName();
        int type = rule.getType();
        int requestType = rule.getRequestMethod();
        String[] keywordTitle = rule.getKeywordTitle();

        //开始解析 防止https拦截 需要伪装为浏览器
        Connection con = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");


        //设置请求类型
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                con.data(params[i], values[i]);
            }
        }

        // 设置请求类型  默认为GET
        Document doc = null;
        try {
            switch (requestType) {
                case Rule.GET:
                    doc = con.timeout(100000).get();
                    break;
                case Rule.POST:
                    doc = con.timeout(100000).post();
                    break;
            }
            //处理返回的数据
            Elements results = new Elements();
            switch (type) {
                case Rule.ID:
                    Element result = doc.getElementById(resultTagName);
                    results.add(result);
                    break;
                //todo id为什么要样？此处存疑！
                //判断传入的a标签的所属的父级类型
                case Rule.CLASS:
                    results = doc.getElementsByClass(resultTagName);
                    break;
                case Rule.SELECTION:
                    results = doc.select(resultTagName);
                    break;
                default:
                    //当resultTagName为空时默认去除body标签
                    if (CheckStringIsNull.checkStringIsNull(resultTagName)) {
                        results = doc.getElementsByTag("body");
                    }
            }

            //获取对应的元素a中的 href 和 text 标签
            for (Element result : results) {
                Elements links = result.getElementsByTag("a");
                for (Element link : links) {
                    //必要筛选
                    String linkHref = link.attr("href");
                    String linkTitle = link.text();
                    LinkTypeData data = new LinkTypeData();
                    //todo
                    //需要改造的地方
                    //只将与Keyword相似的title放入对象
                    if (keywordTitle.length != 0) {
                        for (String Keyword : keywordTitle) {
                            if (linkTitle.contains(Keyword)) {
                                data.setLinkHerf(linkHref);
                                data.setLinkTitle(linkTitle);
                                datas.add(data);
                            }
                        }
                    }
                    //如果关键字数组为空 则获取全部标题
                    else {
                        data.setLinkHerf(linkHref);
                        data.setLinkTitle(linkTitle);
                        datas.add(data);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datas;
    }
}
